import { createRouter, createWebHistory } from 'vue-router'
// 导入 Vuex store 实例，以便在守卫中访问
import store from '../store' 

const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('../views/Home.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Register.vue')
  },
  {
    path: '/profile',
    name: 'Profile',
    component: () => import('../views/Profile.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/deepseek-chat',
    name: 'DeepSeekChat',
    component: () => import('../views/deepseek/ChatInterface.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/deepseek-chat-simple',
    name: 'DeepSeekChatSimple',
    component: () => import('../views/deepseek/DeepSeekChat.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/classes',
    name: 'ClassList',
    component: () => import('../views/class/ClassList.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/classes/create',
    name: 'ClassCreate',
    component: () => import('../views/class/ClassForm.vue'),
    meta: { requiresAuth: true, requiresTeacher: true }
  },
  {
    path: '/classes/:id',
    name: 'ClassDetail',
    component: () => import('../views/class/ClassDetail.vue'),
    props: true,
    meta: { requiresAuth: true }
  },
  {
    path: '/classes/:id/edit',
    name: 'ClassEdit',
    component: () => import('../views/class/ClassForm.vue'),
    props: true,
    meta: { requiresAuth: true, requiresTeacher: true }
  },
  {
    path: '/announcements',
    name: 'AnnouncementList',
    component: () => import('../views/announcement/AnnouncementList.vue')
  },
  {
    path: '/announcements/:id',
    name: 'AnnouncementDetail',
    component: () => import('../views/announcement/AnnouncementDetail.vue'),
    props: true
  },
  {
    path: '/questions',
    name: 'QuestionList',
    component: () => import('../views/question/QuestionList.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/questions/:id',
    name: 'QuestionDetail',
    component: () => import('../views/question/QuestionDetail.vue'),
    props: true,
    meta: { requiresAuth: true }
  },
  {
    path: '/teacher/questions',
    name: 'TeacherQuestionManagement',
    component: () => import('../views/question/QuestionManagement.vue'),
    meta: {
      requiresAuth: true,
      requiresTeacher: true
    }
  },
  {
    path: '/admin',
    name: 'Admin',
    component: () => import('../views/admin/AdminDashboard.vue'),
    meta: { requiresAuth: true, requiresAdmin: true },
    children: [
      {
        path: 'users',
        name: 'UserManagement',
        component: () => import('../views/admin/UserManagement.vue')
      },
      {
        path: 'reset-password',
        name: 'PasswordReset',
        component: () => import('../views/admin/PasswordReset.vue')
      },
      {
        path: 'announcements',
        name: 'AnnouncementManagement',
        component: () => import('../views/admin/AnnouncementManagement.vue')
      },
      {
        path: 'questions',
        name: 'QuestionManagement',
        component: () => import('../views/question/QuestionManagement.vue'),
        meta: { requiresTeacherOrAdmin: true }
      }
    ]
  },
  {
    path: '/reset-password',
    name: 'ResetPassword',
    component: () => import('../views/ResetPassword.vue')
  },
  {
    path: '/exams',
    name: 'ExamList',
    component: () => import('../views/exam/ExamList.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/exams/create',
    name: 'ExamCreate',
    component: () => import('../views/exam/ExamForm.vue'),
    meta: { requiresAuth: true, requiresTeacher: true }
  },
  {
    path: '/exams/:id/edit',
    name: 'ExamEdit',
    component: () => import('../views/exam/ExamForm.vue'),
    props: true,
    meta: { requiresAuth: true, requiresTeacher: true }
  },
  {
    path: '/exams/:id/records',
    name: 'ExamRecordsList',
    component: () => import('../views/exam/ExamRecordsList.vue'),
    props: true,
    meta: { 
      requiresAuth: true, 
      requiresTeacherOrAdmin: true // 教师或管理员可查看记录列表
    }
  },
  {
    path: '/exams/records/:recordId/mark',
    name: 'ExamMarking',
    component: () => import('../views/exam/ExamMarking.vue'),
    props: true,
    meta: { 
      requiresAuth: true, 
      requiresTeacherOrAdmin: true // 教师或管理员可进行批改
    }
  },
  {
    path: '/exams/:id',
    name: 'ExamDetail',
    component: () => import('../views/exam/ExamDetail.vue'),
    props: true,
    meta: { requiresAuth: true }
  },
  {
    path: '/exams/:id/statistics',
    name: 'ExamStatistics',
    component: () => import('../views/exam/ExamStatistics.vue'),
    props: true,
    meta: { requiresAuth: true, requiresTeacherOrAdmin: true }
  },
  {
    path: '/exam/:id/take',
    name: 'ExamTaking',
    component: () => import('../views/exam/ExamTaking.vue'),
    meta: {
      requiresAuth: true,
      requiresStudent: true,
      hideLayout: true
    }
  },
  // --- 学生/教师查看考试记录路由 ---
  {
    path: '/my-records',
    name: 'MyExamRecords',
    component: () => import('../views/exam/MyExamRecords.vue'),
    meta: { requiresAuth: true } // 学生或教师都可访问
  },
  {
    path: '/my-records/:examId',
    name: 'MyExamRecordDetail',
    component: () => import('../views/exam/MyExamRecordDetail.vue'),
    props: true,
    meta: { requiresAuth: true } // 学生或教师都可访问
  },
  // --- 结束新增路由 ---
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('../views/NotFound.vue')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 更新导航守卫
router.beforeEach((to, from, next) => {
  // --- 从 Vuex Store 获取认证状态和用户信息 ---
  const isAuthenticated = store.getters.isAuthenticated;
  // const userInfo = store.getters.getUserInfo; // 移除未使用的 userInfo
  const isAdmin = store.getters.isAdmin;
  const isTeacher = store.getters.isTeacher;
  const isStudent = !store.getters.isAdmin && !store.getters.isTeacher && store.getters.isAuthenticated;
  // --------------------------------------------

  const requiresAuth = to.meta.requiresAuth;
  const requiresAdmin = to.meta.requiresAdmin;
  const requiresTeacher = to.meta.requiresTeacher;
  const requiresTeacherOrAdmin = to.meta.requiresTeacherOrAdmin;
  const requiresStudent = to.meta.requiresStudent;

  // 1. 检查是否需要登录
  if (requiresAuth && !isAuthenticated) {
    next({ name: 'Login', query: { redirect: to.fullPath } });
  } 
  // 2. 检查是否需要管理员权限
  else if (requiresAdmin && !isAdmin) {
    console.warn(`[Auth Guard] Access denied: Route ${to.path} requires Admin role.`);
    next({ name: 'Home' });
  } 
  // 3. 检查是否需要教师权限 (单独)
  else if (requiresTeacher && !isTeacher) {
      console.warn(`[Auth Guard] Access denied: Route ${to.path} requires Teacher role.`);
      next({ name: 'Home' });
  }
  // 4. 检查是否需要教师或管理员权限
  else if (requiresTeacherOrAdmin && !isTeacher && !isAdmin) {
    console.warn(`[Auth Guard] Access denied: Route ${to.path} requires Teacher or Admin role.`);
    next({ name: 'Home' });
  }
  // 5. 检查是否需要学生权限
  else if (requiresStudent && !isStudent) {
      console.warn(`[Auth Guard] Access denied: Route ${to.path} requires Student role.`);
      next({ name: 'Home' });
  }
  // 6. 如果权限都满足，或者页面不需要特殊权限
  else {
    next();
  }
})

export default router 